package cn.com.pansky.action.imgContrast;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import net.vsame.url2sql.action.BaseAction;
import cn.com.pansky.util.HttpUtil;
import cn.com.pansky.util.LssConstant;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ImgContrast extends BaseAction {

	public static double imgContrast(String path1, String path2){
		
		
		
		
		Map<String, String> files = new HashMap<String,String>();
		String u_featrue2 = null ;
		try {
			HttpUtil.enableAllHost();
			files.put("image", path2);
			Map<String, String> params = new HashMap<String, String>();
			//获取人脸特征
			String results = HttpUtil.post(LssConstant.FACE_URL_EXTRACT, params, files);
			JSONObject jsonObjects = (JSONObject) JSONObject.parse(results);
			System.out.println(jsonObjects);
			String faces = jsonObjects.getString("faces");
			JSONArray array = JSONArray.parseArray(faces);
			if (array.size() > 0) {
				jsonObjects = array.getJSONObject(0);
				u_featrue2 = jsonObjects.getString("token");
			} else {
				return 0.0;
			}
		} catch (KeyManagementException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		Map<String, String> files2 = new HashMap<String,String>();
		files2.put("image_idcard", path1);
		Map<String, String> params2 = new HashMap<String, String>();
		params2.put("face_token", u_featrue2 );
		params2.put("name", "");
		params2.put("idcard", "");
		try {
			//比对两张人脸
			String result = HttpUtil.post(LssConstant.FACE_URL_COMPARE, params2, files2);
			JSONObject json = (JSONObject) JSONObject.parse(result);
			JSONObject ss = json.getJSONObject("result_idcard");
			double num = ss.getDouble("confidence");
			return num;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	
	/**
	 * 比对两张照片相似度
	 * @param path1
	 * @param path2
	 * @return
	 */
	public static JSONObject imgCompare( String image_idcard, String image_best, String image_env, String delta){
		Map<String, String> files = new HashMap<String,String>();
		Map<String, String> params = new HashMap<String, String>();
		
		files.put("image_ref1", image_idcard);
		files.put("image_best", image_best);
		files.put("image_env", image_env);
		params.put("comparison_type", "0");  //“0”表示无源比对
		params.put("face_image_type", "meglive");  //“meglive”表示本次比对照为从FaceID活体检测SDK MegLive 中生成的最优照片
		params.put("uuid", "pansky");  //无源比对时，用于标志本次识别对应的用户的id
		params.put("delta", delta);  //在配合MegLive SDK使用时，用于校验上传数据的校验字符串，此字符串会由MegLive SDK直接返回
		
		//比对两张人脸
		JSONObject ret = new JSONObject();
		try {
			//比对两张人脸
			String result = HttpUtil.post(LssConstant.FACE_URL_COMPARE_2, params, files);
			JSONObject json = (JSONObject) JSONObject.parse(result);
			System.out.println("-----调用照片比对结果----"+result);
			String errorMsg = json.getString("error_message");
			if(null != errorMsg) {
				ret.put("code", -100);
				ret.put("confidence", 0.0);
				ret.put("msg", "认证失败，请返回重新认证！");
				return ret;
			}
			JSONObject result_ref1 = json.getJSONObject("result_ref1");
			System.out.println(result_ref1.getString("confidence"));
			Double confidence = result_ref1.getDouble("confidence");

			JSONObject face_genuineness = json.getJSONObject("face_genuineness");
			Double synthetic_face_confidence = face_genuineness.getDouble("synthetic_face_confidence");
			Double synthetic_face_threshold = face_genuineness.getDouble("synthetic_face_threshold");


			if (synthetic_face_confidence < synthetic_face_threshold) {
				System.out.println("不是合成人脸");
			}else{
				ret.put("code", -100);
				ret.put("msg", "认证不通过！");
				return ret;
			}
			Double mask_confidence = face_genuineness.getDouble("mask_confidence");
			Double mask_threshold = face_genuineness.getDouble("mask_threshold");
			if(mask_confidence < mask_threshold) {
				System.out.println("不是面具");
			}else {
				ret.put("code", -100);
				ret.put("msg", "认证不通过！");
				return ret;

			}
			Double screen_replay_confidence = face_genuineness.getDouble("screen_replay_confidence");
			Double screen_replay_threshold = face_genuineness.getDouble("screen_replay_threshold");
			if(screen_replay_confidence < screen_replay_threshold) {
				System.out.println("不是屏幕翻拍");
			}else {
				ret.put("code", -100);
				ret.put("msg", "认证不通过！");
				return ret;
			}
			ret.put("code", 0);
			ret.put("msg", "success");
			ret.put("confidence", confidence);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ret;
		
	}
	
	public static void main(String[] args) {
		
		Map<String, String> files = new HashMap<String,String>();
//		String u_featrue2 = null ;
		try {
//			HttpUtil.enableAllHost();
			files.put("image", "/Users/wangrunhua/Documents/22.JPG");
			Map<String, String> params = new HashMap<String, String>();
			//获取人脸特征
			String results = HttpUtil.post(LssConstant.FACE_URL_EXTRACT, params, files);
			System.out.println(results);
//			JSONObject jsonObjects = (JSONObject) JSONObject.parse(results);
//			System.out.println(jsonObjects);
//			String faces = jsonObjects.getString("faces");
//			JSONArray array = JSONArray.parseArray(faces);
//			if (array.size() > 0) {
//				jsonObjects = array.getJSONObject(0);
//				u_featrue2 = jsonObjects.getString("token");
//			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
//		System.out.println(ImgContrast.imgCompare("/Users/xiangli/Desktop/temp/1.JPG", "/Users/xiangli/Desktop/temp/2.JPG"));
		
	}
}
